<html>
<body>

<form action="query.php" method="POST">
<textarea name ="input" cols = 40 rows =6></textarea>
<input type="submit" />
</form>

<?php
if($_POST["input"])
{
	$db_connection = mysql_connect("localhost", "cs143", "");
	mysql_select_db("CS143", $db_connection);

	if(!$db_connection) 
	{
    		$errmsg = mysql_error($db_connection);
    		print "Connection failed: $errmsg <br />";
    		exit(1);
	}

	$query = $_POST["input"];
	$rs = mysql_query($query, $db_connection);

	echo "<b> Results from MySQL: </b></br>";

	Print "<table border cellpadding=3>";
	for($i = 0; $i < mysql_num_fields($rs); $i++)
	{
		$fieldName = mysql_field_name($rs, $i);
		Print "<th>$fieldName</th>";

	}
	while($row = mysql_fetch_row($rs)) 
	{
		Print "<tr>";
		for($j = 0; $j < mysql_num_fields($rs); $j++)
		{
			if($row[$j] != NULL)
				Print "<td>".$row[$j] . "</td>";
			else
				Print "<td>"."N/A" . "</td>";
		}
		Print "</tr>";
	}
	Print "</table>"; 

	mysql_close($db_connection);
}




?>

</body>
</html>